package com.beiye.drivertransport.safelearn;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.SubActivity.SignActivity;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.CheckBean;
import com.beiye.drivertransport.bean.DaiKaoSureBean;
import com.beiye.drivertransport.bean.DaikaoAndHavetestCourseBean;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.bean.MineUseBean;
import com.beiye.drivertransport.bean.QuestionInvestigationBean;
import com.beiye.drivertransport.bean.UpPhotoBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.examination.adapter.QuestionTopicAdapter;
import com.beiye.drivertransport.examination.adapter.TopicAdapter;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.CustomProgressDialog;
import com.beiye.drivertransport.utils.LogUtils;
import com.beiye.drivertransport.utils.MessageEventTest2;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.ToastUtil;
import com.beiye.drivertransport.utils.UserManger;
import com.beiye.drivertransport.view.LinePathView;
import com.example.mylibrary.paper.view.QuestionViewPager;
import com.example.mylibrary.paper.view.SlidingUpPanelLayout;
import com.githang.statusbar.StatusBarCompat;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.w3c.dom.Text;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Copyright (C)
 * Author: 刘学章
 * Date: 2021-3-30 14:03:36
 * Description:
 */
public class QuestionInvestigationActivity extends TwoBaseAty implements View.OnClickListener, SurfaceHolder.Callback {
    

    ImageView imgExaminationback;

    QuestionViewPager questionViewPager;

    ImageView shadowView;

    TextView bt_pre;

    TextView bt_next;

    RecyclerView recyclerView;

    LinearLayout dragView;

    SlidingUpPanelLayout mLayout;

    private QuestionTopicAdapter topicAdapter;
    ArrayList<Integer> snlist = new ArrayList<>();
    private List snlist1;
    private PopupWindow msignPopwindow;
    private LinePathView signatureview;
    private Camera mCamera;
    private ProgressDialog mProgressDialog;
    private SurfaceHolder mHolder;
    private int prePosition;
    private int curPosition;
    GridLayoutManager gridLayoutManager;

    public ArrayList<QuestionInvestigationBean.DataBean.UqItemListBean> testlist = new ArrayList<>();
    final File fileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Signname");

    private static final int USER_QUESTION_DETAIL = 1;
    private static final int MOD_USER_QUESTION_REQ = 3;
    private int relSn=373278;
    private String orgId;
    private int signMark;
    private CustomProgressDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_question_investigation;

    }


    @Override
    public void initData() {
        imgExaminationback = (ImageView) findViewById(R.id.img_examinationback);
        questionViewPager = (QuestionViewPager) findViewById(R.id.readerViewPager);
        shadowView = (ImageView) findViewById(R.id.shadowView);
        bt_pre = (TextView) findViewById(R.id.bt_pre);
        bt_next = (TextView) findViewById(R.id.bt_next);
        recyclerView = (RecyclerView) findViewById(R.id.list);
        dragView = (LinearLayout) findViewById(R.id.dragView);
        mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        //注册EventBus
        EventBus.getDefault().register(this);

        Bundle bundle=getIntent().getExtras();
        relSn=bundle.getInt("relSn");
        orgId=bundle.getString("orgId");
        signMark=bundle.getInt("signMark");

        initSlidingUoPanel();
        initList();
    }


    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.img_examinationback) {
            TiShiDialog.Builder builder = new TiShiDialog.Builder(this);
            builder.setMessage("是否离开问卷调查");
            builder.setTitle("提示:");
            builder.setPositiveButton("离开", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                    //设置你的操作事项
                }
            });
            builder.setNegativeButton("取消",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            builder.create().show();
        } else if (id == R.id.bt_pre) {
            preQuestion();
            bt_next.setText("确认答案，下一题");
        } else if (id == R.id.bt_next) {
            SharedPreferences sp = getSharedPreferences("opition", Context.MODE_PRIVATE);
            int qusn = sp.getInt("sn", 0);
            String checkboxopition = sp.getString("checkboxopition" + qusn, "");
            String radioopition = sp.getString("radioopition" + qusn, "");
            String editoption = sp.getString("editoption" + qusn, "");
            String strsure = bt_next.getText().toString().trim();
            if (strsure.equals("确认答案，下一题")) {
                if (!checkboxopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    modUserQuestion(qusn, sortString(checkboxopition));
                } else if (!radioopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    modUserQuestion(qusn, radioopition);
                } else if (!editoption.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    modUserQuestion(qusn, editoption);
                } else {
                    TiShiDialog.Builder builder1 = new TiShiDialog.Builder(QuestionInvestigationActivity.this);
                    builder1.setMessage("请先答题");
                    builder1.setTitle("提示:");
                    builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder1.create().show();
                    return;
                }
            } else if (strsure.equals("确认答案，最后一题")) {
                if (!checkboxopition.equals("")) { //复选选项
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    modUserQuestion(qusn, sortString(checkboxopition));
                } else if (!radioopition.equals("")) { //单选选项
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    modUserQuestion(qusn, radioopition);
                } else if (!editoption.equals("")) { //输入框选项
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    modUserQuestion(qusn, editoption);
                } else {
                    TiShiDialog.Builder builder1 = new TiShiDialog.Builder(QuestionInvestigationActivity.this);
                    builder1.setMessage("请先答题");
                    builder1.setTitle("提示:");
                    builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder1.create().show();
                    return;
                }
            } else if (strsure.equals("提交问卷")) {
                if (!checkboxopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    modUserQuestion(qusn, sortString(checkboxopition));
                } else if (!radioopition.equals("")) {
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    modUserQuestion(qusn, radioopition);
                } else if (!editoption.equals("")) { //输入框选项
                    SharedPreferences.Editor edit2 = sp.edit();
                    edit2.putInt("checksn", qusn);
                    edit2.commit();
                    modUserQuestion(qusn, editoption);
                }
                if (signMark >= 1) {
                    showUserSignPopwindow();//签字
                } else {
                    finish();
                }
            }
        }
    }

    @Override
    public void requestData() {
        //问卷详情
        getUserQuestionDetail();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MessageEventTest2 event) {
        int sn = event.getSn();
        String checkboxopition = event.getCheckboxopition();
        String radioopition = event.getRadioopition();
        String editoption = event.getEditoption();
        LogUtils.e("测试", "checkboxoption: " + checkboxopition + " radiooption: " + radioopition + " editoption: " + editoption);

        //承接监听返回值
        SharedPreferences sp = getSharedPreferences("opition", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt("sn", sn);
        edit.putString("checkboxopition" + sn, checkboxopition);
        edit.putString("radioopition" + sn, radioopition);
        edit.putString("editoption" + sn, editoption);
        edit.commit();
    }

    //弹窗签名
    @SuppressLint("HandlerLeak")
    private void showUserSignPopwindow() {
        View contentView = LayoutInflater.from(QuestionInvestigationActivity.this).inflate(R.layout.sign_popwindowlayout, null);
        msignPopwindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        LinearLayout le_takepicture = (LinearLayout) contentView.findViewById(R.id.le_takepicture);//
        msignPopwindow.setFocusable(false);
        msignPopwindow.setOutsideTouchable(false);
        msignPopwindow.showAtLocation(le_takepicture, Gravity.CENTER, 0, 0);

        //签名
        signatureview = (LinePathView) contentView.findViewById(R.id.signatureview);
        TextView tvHint=(TextView)contentView.findViewById(R.id.pw_sign_tv_hintMsg);
        //重签
        TextView tv_resign = (TextView) contentView.findViewById(R.id.tv_violation1);
        TextView tv_sure = (TextView) contentView.findViewById(R.id.tv_sure);//确定

        tvHint.setVisibility(View.GONE);

        tv_resign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signatureview.clear();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    //检测是否有写的权限
                    int permission = ActivityCompat.checkSelfPermission(QuestionInvestigationActivity.this,
                            "android.permission.WRITE_EXTERNAL_STORAGE");
                    if (permission != PackageManager.PERMISSION_GRANTED) {
                        ToastUtil.showShortToast(QuestionInvestigationActivity.this, "请到手机设置界面里找驾运宝允许开启读写手机存储,否则不能点确定");
                        return;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                save(fileDir);

            }
        });


    }


    @Override
    protected void onPause() {
        super.onPause();
        /**
         * 记得释放camera，方便其他应用调用
         */
        releaseCamera();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (this.checkCameraHardware(this) && (mCamera == null)) {
            // 打开camera
            mCamera = getCamera();
            if (mHolder != null) {
                setStartPreview(mCamera, mHolder);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //摄像头
        int cameraCount = 1;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();// 得到摄像头的个数

        for (int i = 0; i < cameraCount; i++) {
            Camera.getCameraInfo(i, cameraInfo);// 得到每一个摄像头的信息
            // 现在是后置，变更为前置
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                /**
                 * 记得释放camera，方便其他应用调用
                 */
                releaseCamera();
                // 打开当前选中的摄像头
                mCamera = Camera.open(i);
                //   mCamera.setDisplayOrientation(90);//旋转度数
                // 通过surfaceview显示取景画面
                setStartPreview(mCamera, mHolder);

            }
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    @Override
    public void onBackPressed() {
        TiShiDialog.Builder builder = new TiShiDialog.Builder(this);
        builder.setMessage("是否离开问卷调查");
        builder.setTitle("提示:");
        builder.setPositiveButton("离开", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
                //设置你的操作事项
            }
        });
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();

        if (mLayout != null &&
                (mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED || mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.ANCHORED)) {
            mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
        } else {
            //   super.onBackPressed();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        setStartPreview(mCamera, mHolder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.
        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }
        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }
        setStartPreview(mCamera, mHolder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // 当surfaceview关闭时，关闭预览并释放资源
        /**
         * 记得释放camera，方便其他应用调用
         */
        releaseCamera();
        holder = null;
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if (what == USER_QUESTION_DETAIL) {
            LogUtils.e("测试", "onSuccess: "+result );
            QuestionInvestigationBean questionBean = JSON.parseObject(result, QuestionInvestigationBean.class);
            LogUtils.e("测试", "onSuccess: "+questionBean.getCode()+" " );
            List<QuestionInvestigationBean.DataBean.UqItemListBean> rowslist = questionBean.getData().getUqItemList();
            LogUtils.e("测试", "onSuccess: "+rowslist.size() );
            testlist.clear();
            testlist.addAll(rowslist);
            for (int j = 0; j < testlist.size(); j++) {
                QuestionInvestigationBean.DataBean.UqItemListBean temp = testlist.get(j);
                //temp.setSeqNo(j + 1);
                if (testlist.get(j).getFillType() == 3 || testlist.get(j).getFillType() == 1) { //单选和判断
                    if (testlist.get(j).getUserOption() == null) {
                        testlist.get(j).setQuestion_select(-1);
                    } else if (testlist.get(j).getUserOption().equals("A")) {
                        testlist.get(j).setQuestion_select(0);
                    } else if (testlist.get(j).getUserOption().equals("B")) {
                        testlist.get(j).setQuestion_select(1);
                    } else if (testlist.get(j).getUserOption().equals("C")) {
                        testlist.get(j).setQuestion_select(2);
                    } else if (testlist.get(j).getUserOption().equals("D")) {
                        testlist.get(j).setQuestion_select(3);
                    } else if (testlist.get(j).getUserOption().equals("E")) {
                        testlist.get(j).setQuestion_select(4);
                    } else if (testlist.get(j).getUserOption().equals("F")) {
                        testlist.get(j).setQuestion_select(5);
                    }
                } else if (testlist.get(j).getFillType() == 2) { //多选
                    if (testlist.get(j).getUserOption() == null) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCDEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCDE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCDF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABDEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCDEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCD")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABDE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACDE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACDF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ADEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABCF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABDF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCDE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCDF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BDEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("CDEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABC")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABD")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ABF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACD")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ACF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ADE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("ADF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("AEF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCD")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BCF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BDE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BDF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("BEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("CDE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("CDF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("CEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("DEF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("AB")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("AC")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (sortString(testlist.get(j).getUserOption()).equals("AD")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("AE")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("AF")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("BC")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("BD")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("BE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("BF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("CD")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("CE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("CF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("DE")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("DF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("EF")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(5);
                    } else if (testlist.get(j).getUserOption().equals("A")) {
                        testlist.get(j).setQuestion_select1(0);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("B")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("C")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(2);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("D")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(3);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("E")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(4);
                        testlist.get(j).setQuestion_select6(-1);
                    } else if (testlist.get(j).getUserOption().equals("F")) {
                        testlist.get(j).setQuestion_select1(-1);
                        testlist.get(j).setQuestion_select2(-1);
                        testlist.get(j).setQuestion_select3(-1);
                        testlist.get(j).setQuestion_select4(-1);
                        testlist.get(j).setQuestion_select5(-1);
                        testlist.get(j).setQuestion_select6(5);
                    }
                } else if (testlist.get(j).getFillType() == 4) {
                    if (testlist.get(j).getUserOption()==null) {
                        testlist.get(j).setQuestion_select1(-1);
                    } else {
                        testlist.get(j).setQuestion_select1(1);
                    }
                }
            }

            if (topicAdapter != null) {
                topicAdapter.setDataNum(testlist.size());
                topicAdapter.setDatas(testlist);
            }
            initReadViewPager(testlist);
        } else if (what == MOD_USER_QUESTION_REQ) {
            nextQuestion();
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
            String msg = checkBean.getMsg();
            if (code == 0) {
                SharedPreferences sp = getSharedPreferences("opition", Context.MODE_PRIVATE);
                int sn = sp.getInt("checksn", 0);
                snlist.add(sn);
                snlist1 = removeDuplicate(snlist);
                int num = snlist1.size();
                if (sn == testlist.get(testlist.size() - 1).getSn()) {
                    bt_next.setText("提交问卷");
                } else if (sn == testlist.get(testlist.size() - 2).getSn()) {
                    if (num != testlist.size()) {
                        bt_next.setText("确认答案，最后一题");
                    } else {
                        bt_next.setText("提交问卷");
                    }
                } else {
                    bt_next.setText("确认答案，下一题");
                }
                for (int i = 0; i < testlist.size(); i++) {
                    if (testlist.get(i).getSn() == sn) {
                        testlist.get(i).setSelect(true);
                    }
                }
                if (topicAdapter != null) {
                    topicAdapter.setDataNum(testlist.size());
                    topicAdapter.setDatas(testlist);
                }
            }
        }
        super.onSuccess(result, call, response, what);
    }
    @Override
    public void onError(String result, Call call, Response response, int what) {
        if (what == 3) {
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            //     int code = checkBean.getCode();
            String msg = checkBean.getMsg();
            TiShiDialog.Builder builder = new TiShiDialog.Builder(this);
            builder.setMessage(msg);
            builder.setTitle("提示:");
            builder.setPositiveButton("我知道啦", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            });
            builder.create().show();

        }

        super.onError(result, call, response, what);
    }


    //自定义方法
    //初始化
    private void initSlidingUoPanel() {
        int height = getWindowManager().getDefaultDisplay().getHeight();
        SlidingUpPanelLayout.LayoutParams params = new SlidingUpPanelLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (height * 0.5f));
        dragView.setLayoutParams(params);
        mLayout.addPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {

            }
            @Override
            public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) {
                if (newState == SlidingUpPanelLayout.PanelState.COLLAPSED) {
                    MoveToPosition();
                }
            }
        });
        mLayout.setFadeOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
            }
        });
    }
    private void initList() {
        recyclerView = (RecyclerView) findViewById(R.id.list);

        gridLayoutManager = new GridLayoutManager(this, 8);
        recyclerView.setLayoutManager(gridLayoutManager);
        topicAdapter = new QuestionTopicAdapter(this);
        recyclerView.setAdapter(topicAdapter);


        topicAdapter.setOnTopicClickListener(new QuestionTopicAdapter.OnTopicClickListener() {
            @Override
            public void onClick(QuestionTopicAdapter.TopicViewHolder holder, int position) {
                curPosition = position;
                if (mLayout != null &&
                        (mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED || mLayout.getPanelState() == SlidingUpPanelLayout.PanelState.ANCHORED)) {
                    mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
                }

                if (position == testlist.size() - 1) {
                    bt_next.setText("提交问卷");
                } else {
                    bt_next.setText("确认答案，下一题");
                }

                questionViewPager.setCurrentItem(position);

                topicAdapter.notifyCurPosition(curPosition);
                topicAdapter.notifyPrePosition(prePosition);
                prePosition = curPosition;
            }
        });

    }
    private void initReadViewPager(final ArrayList<QuestionInvestigationBean.DataBean.UqItemListBean> testlist) {
        questionViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                QuestionInvestigationBean.DataBean.UqItemListBean rowsBean = testlist.get(position);
                QuestionInvestigationFragment fragment = QuestionInvestigationFragment.newInstance(rowsBean, position);
                return fragment;
            }

            @Override
            public int getCount() {
                return testlist.size();
            }
        });
        questionViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                shadowView.setTranslationX(questionViewPager.getWidth() - positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {

                curPosition2 = position;
                topicAdapter.notifyCurPosition(curPosition2);
                topicAdapter.notifyPrePosition(prePosition2);

                prePosition2 = curPosition2;
                MoveToPosition();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    /**
     * 滚动 recycleView 到当前选择的位置
     */
    private void MoveToPosition() {
        gridLayoutManager.scrollToPositionWithOffset(curPosition2, 0);
    }

    /**
     * 上一题
     */
    private synchronized void preQuestion() {
        int currentItem = questionViewPager.getCurrentItem();
        currentItem = currentItem - 1;
        if (currentItem > testlist.size() - 1) {
            currentItem = testlist.size() - 1;
        }
        questionViewPager.setCurrentItem(currentItem);
    }

    /**
     * 下一题
     */
    private synchronized void nextQuestion() {
        int currentItem = questionViewPager.getCurrentItem();
        currentItem = currentItem + 1;
        if (currentItem < 0) {
            currentItem = 0;
        }
        questionViewPager.setCurrentItem(currentItem);
    }

    private Camera getCamera() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            // Camera is not available (in use or does not exist)
            camera = null;
            // LogUtils.e(TAG, "Camera is not available (in use or does not exist)");
        }
        return camera;
    }
    /**
     * 释放mCamera
     */
    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();// 停掉原来摄像头的预览
            mCamera.release();
            mCamera = null;
        }
    }
    /**
     * Check if this device has a camera
     */
    private boolean checkCameraHardware(Context context) {
        if (context.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }
    /**
     * 设置camera显示取景画面,并预览
     *
     * @param camera
     */
    private void setStartPreview(Camera camera, SurfaceHolder holder) {
        try {
            camera.setPreviewDisplay(holder);
            //开始预览
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (IOException e) {
            // Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

    //字母排序
    public String sortString(String str) {

        //  System.out.println("排序前的字符串是:" + str);
        LogUtils.e("排序前的字符串是:", str);
        // 排序前字符串转换为字符数组
        char[] cs = str.toCharArray();

        // 升序排序
        for (int i = 0; i < cs.length - 1; i++) {
            for (int j = 0; j < cs.length - 1 - i; j++) {

                char temp = 0;
                if (cs[j] > cs[j + 1]) {

                    temp = cs[j];
                    cs[j] = cs[j + 1];
                    cs[j + 1] = temp;
                }
            }
        }

        // 排序以后的字符串
        String newString = "";

        for (int i = 0; i < cs.length; i++) {
            newString += cs[i];
        }
        // 打印排序以后的字符串
        LogUtils.e("排序后的字符串是:", newString);
        return newString;

    }
    public static List removeDuplicate(List list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    private int prePosition2;
    private int curPosition2;





    /**
     * 得到图片的路径 以及图片的名字
     */
    private String getFilePath(File fileDir) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        final String fileName = simpleDateFormat.format(new Date()) + ".png";
        LogUtils.e("filename", "---" + fileName);
        File file = new File(fileDir, fileName);
        return file.getAbsolutePath();
    }

    /**
     * 保存图片
     */
    private void save(File fileDir) {
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        final String fileName = simpleDateFormat.format(new Date()) + ".png";
        File savefile = new File(fileDir, fileName);
        final String filePath = getFilePath(fileDir);
        if (!signatureview.getTouched()) //是否签名
        {
            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(QuestionInvestigationActivity.this);
            builder1.setMessage("请完善您的签名");
            builder1.setTitle("提示:");
            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    ;
                    //设置你的操作事项
                }
            });
            builder1.create().show();
            return;
        }
        msignPopwindow.dismiss();
        try {
            signatureview.save(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        uploadImg(savefile);//上传服务器封装方法

    }

    //上传图片
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    private final OkHttpClient client = new OkHttpClient();
    private void uploadImg(File mediaFile) {
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        String userName = data.getUserName();
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if (mediaFile != null) {
            builder.addFormDataPart("signImg", mediaFile.getName(), RequestBody.create(MEDIA_TYPE_PNG, mediaFile));
            builder.addFormDataPart("sn", relSn+"");
            builder.addFormDataPart("userName", userName);
            builder.addFormDataPart("tSignMark", 1 + "");
        }
        MultipartBody requestBody = builder.build();
        //showLoadingDialog("正在加载");
        dialog = CustomProgressDialog.show(QuestionInvestigationActivity.this, "保存中...", true, null);
        //构建请求
        Request request = new Request.Builder()
                .url(AppInterfaceConfig.BASE_URL + "app/course/uploadSignFromApp")//地址
                .post(requestBody)//添加请求体
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                LogUtils.e("上传失败", e.getLocalizedMessage());
                dialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                LogUtils.e("上传成功", str);
                UpPhotoBean upPhotoBean = JSON.parseObject(str, UpPhotoBean.class);
                boolean result = upPhotoBean.isResult();
                String msg = upPhotoBean.getMsg();
                dialog.dismiss();
                if (result) {
                    finish();
                } else {
                    TiShiDialog.Builder builder1 = new TiShiDialog.Builder(QuestionInvestigationActivity.this);
                    builder1.setMessage(msg);
                    builder1.setTitle("提示:");
                    builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            //设置你的操作事项
                        }
                    });
                    builder1.create().show();
                }


            }
        });
        /*SharedPreferences sp = getSharedPreferences("QuestionInvestigationActivity",
                Context.MODE_PRIVATE);
        String orgId = sp.getString("orgId", "");
        // mImgUrls为存放图片的url集合
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

        if (mediaFile != null) {
            builder.addFormDataPart("psignImg", mediaFile.getName(), RequestBody.create(MEDIA_TYPE_PNG, mediaFile));
            builder.addFormDataPart("sn", sn + "");
        }
        MultipartBody requestBody = builder.build();
        mProgressDialog = new ProgressDialog(QuestionInvestigationActivity.this, ProgressDialog.THEME_HOLO_DARK);
        mProgressDialog.setMessage("提交中...");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
        //构建请求
        Request request = new Request.Builder()
                .url(AppInterfaceConfig.BASE_URL + "app/paper/finishMtForT")//地址
                .post(requestBody)//添加请求体
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                LogUtils.e("上传失败", e.getLocalizedMessage());
                mProgressDialog.dismiss();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                LogUtils.e("上传成功", str);
                DaiKaoSureBean sureBean = JSON.parseObject(str, DaiKaoSureBean.class);
                DaiKaoSureBean.DataBean data = sureBean.getData();
                mProgressDialog.dismiss();
            }
        });*/

    }



    //网络请求
    //问卷详情
    private void getUserQuestionDetail() {
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        new Login().userQuestionFindDetail(relSn, data.getUserId(), orgId, this, USER_QUESTION_DETAIL);
    }
    //回答问卷
    private void modUserQuestion(int qusn, String str) {
        new Login().userQuestionModBySn(qusn, str, this, MOD_USER_QUESTION_REQ);//复选题
    }

}
